﻿//https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_dialog_definition.html
CKEDITOR.plugins.add('dcfile', {
    init: function (editor) {
        editor.ui.addButton('dcfile',{
            label: '文件',
            icon: this.path + 'images/file.svg',
            click: function(){
                daicuo.ajax.get(
                    //加载上传表单
                    daicuo.config.root+'ckeditor/upload/index',
                    //加载上传表单成功
                    function(data, status, xhr){
                        //展示上传表单
                        daicuo.bootstrap.dialogForm(data);
                        //加载上传组件
                        CKEDITOR.DCFILE(editor);
                    },
                    //加载上传表单失败
                    function(xhr, status, data){
                        daicuo.bootstrap.dialogForm(xhr);
                    }
                );
            }
        });
    }
});
//扩展
CKEDITOR.DCFILE = function(editor){
    var uploadConfig = $.extend({
        element: '#daicuoUpload #btn-upload',
        multiple: true,
        mimeTypes: 'application/zip',
        onInit: function(up){
            $(document).on('change', '#score', function(){
                up.setOption("multipart_params", {
                    score : $(this).val()
                });
            });
        },
        onError: function(up, data){
            daicuo.bootstrap.dialog(data.msg);
        },
        onSuccess: function(up, file, xhr){
            //文件名
            if(file.responseTp.data.score){
                var fileName = file.responseTp.data.old_name + '（需要'+file.responseTp.data.score+'个积分）';
            }else{
                var fileName = file.responseTp.data.old_name;
            }
            //插入编辑器
            if(file.responseTp.data.urlId){
                editor.insertHtml('<a class="dc-file" href="'+file.responseTp.data.urlId+'">'+fileName+'</a><br />');
            }else{
                editor.insertHtml('<a class="dc-file" href="'+file.responseTp.data.url+'">'+fileName+'</a><br />');
            }
            //隐藏dialog
            $(".dc-modal").modal("hide");
        }
    },$('#daicuoUpload').data('config'));
    //监听上传按钮
    daicuo.upload.ajaxLoad(function(){
        daicuo.upload.start(uploadConfig);
    });
};